#include<bits/stdc++.h>
using namespace std;
#define int long long
int ksm(int a,int q){
    int res=1;
    while(q){
        if(q&1)res=res*a;
        a=a*a;
        q>>=1;
    }
    return res;
}
int T,n,m,ap[100005],a[100005],Q[100005];
struct node{
    int id,R;//id:number of man  R:turns
    int rsum;
    vector<int>okv,okid;
    node(){}
    node(int id,int R,int rsum,vector<int> okv,vector<int> okid):id(id),R(R),rsum(rsum),okv(okv),okid(okid){}
};
queue<node>q;
string s[20];
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    freopen("arena.in","r",stdin);
    freopen("arena.out","w",stdout);
    cin>>n>>m;
    for(int i=1;i<=n;i++)cin>>ap[i];
    for(int i=1;i<=m;i++)cin>>Q[i];
    int K=1;
    while(ksm(2,K)<n)K++;
    for(int i=1;i<=K;i++)cin>>s[i];
    cin>>T;
    int kkk[4];
    while(T--){
        cin>>kkk[0]>>kkk[1]>>kkk[2]>>kkk[3];
        for(int i=1;i<=n;i++)a[i]=ap[i]^kkk[i%4];
        int ans=0;
        for(int i=1;i<=m;i++){
            while(q.size())q.pop();

            int k=1;
            while(k<Q[i])k*=2;
            for(int j=1;j<=Q[i];j++)q.push(node(j,1,0,{a[j]},{j}));
            for(int j=Q[i]+1;j<=k;j++)q.push(node(j,1,j,{},{}));
            while(q.size()>1){
                node L=q.front();q.pop();
                node R=q.front();q.pop();
                node D=node(R.id/2,R.R+1,0,{},{});
                // cout<<"         "<<D.id<<"_"<<s[D.R-1][D.id-1]<<endl;
                if(s[D.R-1][D.id-1]=='0'){
                    // cout<<"* ";
                    if(L.rsum){
                        for(int j=0;j<L.okv.size();j++){
                            if(L.okv[j]>=R.R)D.okid.push_back(L.okid[j]),D.okv.push_back(L.okv[j]);
                        }
                        D.rsum=L.rsum+R.rsum;
                    }
                    else{
                        bool flag=0;
                        for(int j=0;j<L.okv.size();j++){
                            // cout<<L.okv[j]<<" ";
                            if(L.okv[j]>=R.R)D.okid.push_back(L.okid[j]),D.okv.push_back(L.okv[j]);
                            else flag=1;
                        }
                        // cout<<endl;
                        if(flag){
                            // cout<<"|||";
                            for(int j=0;j<R.okv.size();j++)D.okid.push_back(R.okid[j]),D.okv.push_back(R.okv[j]);
                            D.rsum=R.rsum;
                        }
                    }
                }
                else{
                    if(R.rsum){
                        for(int j=0;j<L.okv.size();j++){
                            D.okid.push_back(L.okid[j]),D.okv.push_back(L.okv[j]);
                        }
                        for(int j=0;j<R.okv.size();j++)
                            if(R.okv[j]>=R.R)D.okid.push_back(R.okid[j]),D.okv.push_back(R.okv[j]);
                        D.rsum=L.rsum+R.rsum;
                    }
                    else{
                        bool flag=0;
                        for(int j=0;j<R.okv.size();j++){
                            if(R.okv[j]>=R.R)D.okid.push_back(R.okid[j]),D.okv.push_back(R.okv[j]);
                            else flag=1;
                        }
                        if(flag){
                            for(int j=0;j<L.okv.size();j++)D.okid.push_back(L.okid[j]),D.okv.push_back(L.okv[j]);
                            D.rsum=L.rsum;
                        }
                    }
                }
                // cout<<D.id<<" "<<D.R<<" "<<D.rsum<<endl;
                q.push(D);
            }
            node D=q.front();q.pop();
            int sum=D.rsum;
            for(int j=0;j<D.okid.size();j++)sum+=D.okid[j];
            // cout<<endl<<"ans:"<<sum<<endl;
            ans^=i*sum;
        }
        cout<<ans<<endl;
    }
    return 0;
}
/*
5 5
0 0 0 0 0
5 4 1 2 3
1001
10
1
1
2 1 0 0
*/